package com.choudou5.solr.framework.util;

import com.choudou5.solr.framework.bean.UserSessionBean;
import com.choudou5.solr.framework.constants.UserConsts;
import com.choudou5.solr.framework.holder.SessionContextHolder;

/**
 * @name 用户工具类
 * @author choudou5
 * @date 2018/7/18
 */
public class UserUtil {

    public static UserSessionBean getUser() {
        return SessionContextHolder.getUserSessionBean();
    }

    public static Integer getUserId() {
        UserSessionBean user = SessionContextHolder.getUserSessionBean();
        if(user != null)
            return user.getId();
        return null;
    }

    public static String getUserIdStr() {
        UserSessionBean user = SessionContextHolder.getUserSessionBean();
        if(user != null)
            return user.getId().toString();
        return null;
    }

    public static String getUserName() {
        UserSessionBean user = SessionContextHolder.getUserSessionBean();
        if(user != null)
            return user.getName();
        return null;
    }

    public static boolean isAdmin() {
        UserSessionBean user = SessionContextHolder.getUserSessionBean();
        if(user != null)
            return UserConsts.UserRole.ADMIN.eq(user.getRole()) || UserConsts.UserRole.SUPER_ADMIN.eq(user.getRole());
        return false;
    }

    public static boolean isSuperAdmin() {
        UserSessionBean user = SessionContextHolder.getUserSessionBean();
        if(user != null)
            return UserConsts.UserRole.SUPER_ADMIN.eq(user.getRole());
        return false;
    }

    public static void removeUserSession() {
        UserSessionBean user = SessionContextHolder.getUserSessionBean();
        if(user != null){
            SessionContextHolder.removeUserSessionBean();
        }
    }

}
